<?php
require_once('sub.inc.php');

if($_SERVER['HTTP_HOST'] || $_SERVER['SERVER_NAME']){
	die;
}

getLatest();
updateOlder();
cleanUpRedir();


/************************
* Functions
************************/

function logg($msg){
	print $msg."\n";
}

function getAndClean($url){
	//Get raw wikitext
	logg("getAndClean($url)");
	$url = INDEX_ROOT."action=raw&title=".$url;
	$res = curlIt($url);
	//$res = utf8_decode($res);
	//$res = utf8_encode($res);

	//Ungracefully rip out wikitext (currently supports 3 levels of {{ }} recursion)
	$res = preg_replace("/{{(?:[^{}]|{{(?:[^{}]|{{[^{}]*}})*}})*}}/siU","",$res);
	$res = preg_replace("/\[\[File:(.*)\]\]/mi","",$res);
	$res = preg_replace("/\[\[Image:(.*)\]\]/mi","",$res);
	$res = preg_replace("/\[\[Category:(.*)\]\]/i","",$res);
	$res = preg_replace("/\[\[(.*)\|(.*)\]\]/iU","$2",$res);
	$res = preg_replace("/\[\[(.*):(.*)\]\]/iU","",$res);
	$res = preg_replace("/\[\[(.*)\]\]/iU","$1",$res);
	$res = preg_replace("/==(.*)==/","",$res);
	$res = preg_replace("/\*(\s*)$/","",$res);
	$res = preg_replace("/\n\*(\s)/","\n",$res);
	$res = preg_replace("/\n\*/","\n",$res);
	$res = preg_replace("/\n\n\n/","\n",$res);
	$res = preg_replace("/\n\n\n/","\n",$res);

	//Return most clean text
	return $res;
}

function updateIt($id,$revid,$url){
	logg("updateIt($id,$revid,$url)");
	$text = mEscape(getAndClean($url));
	//$text = utf8_encode($text);
	$url = mEscape($url);
	$sql = "UPDATE `enwn_articles` SET `fulltext`='$text',`revid`='$revid',`last_update`=CURRENT_TIMESTAMP,`last_check`=CURRENT_TIMESTAMP WHERE `id`=$id";
	logg($sql);
	$res = mysqli_query($GLOBALS['con'], $sql);
	logg(mysqli_error($GLOBALS['con']));
}

function markCheck($id){
	logg("markCheck($id)");
	$res = mysqli_query($GLOBALS['con'], "UPDATE `enwn_articles` SET `last_check`=CURRENT_TIMESTAMP WHERE `id`=$id");
	logg(mysqli_error($GLOBALS['con']));
}

function populateIt($url){
	logg("populateIt($url)");
	$text = mEscape(getAndClean($url));
	$aurl = API_ROOT."action=parse&format=json&page=".$url;
	$web = curlIt($aurl);
	$webRay = json_decode($web);
	$title = mEscape($webRay->parse->displaytitle);
	$url = mEscape($url);
	
	$sql = "INSERT INTO `enwn_articles` (`title`,`url`,`fulltext`,`revid`,`last_update`,`last_check`) VALUES ('$title','$url','$text',{$webRay->parse->revid},CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)";
	$res = mysqli_query($GLOBALS['con'], $sql);
	logg($sql);
	logg(mysqli_error($GLOBALS['con']));
}

function mEscape($text){
	$text = mysqli_real_escape_string($GLOBALS['con'],$text);
	return($text);
}

function getLatest() {
	logg("getLatest()");
	$url = API_ROOT;
	$post = "action=login&lgname=".BOT_USER."&lgpassword=".BOT_PASS."&format=json";
	$res = curlIt($url,true,$post);

	$post = "action=purge&titles=Template:Main_headlines&format=json";
	$res = curlIt($url,true,$post);

	$url = API_ROOT."action=parse&format=json&page=Template:Main_headlines";
	$res = curlIt($url);

	$fullRay = json_decode($res);
	$fullText = $fullRay->parse->text->{'*'};
//	$fullText = utf8_decode($fullText);
	preg_match_all("/=\"\/wiki\/(.*)\?dpl_id=(.*)title=\"(.*)\">/",$fullText,$bunchesLinks);
	$oldest = array_reverse($bunchesLinks[1]);

	for($a = 0; $a<count($oldest);$a++){
		investigateIt($oldest[$a]);
	}
} // end getLatest()

function investigateIt($baseurl){
	logg("investigateIt($baseurl)");
	$sql = "SELECT * FROM `enwn_articles` WHERE `url`='".mEscape($baseurl)."'";
	logg($sql);
	$res = mysqli_query($GLOBALS['con'], $sql);
	logg(mysqli_error($GLOBALS['con']));
	if($row = mysqli_fetch_array($res)){
		//Article already in DB
		$url = API_ROOT."action=parse&format=json&page=".$baseurl;
		$web = curlIt($url);
		$webRay = json_decode($web);
		if($webRay->parse->revid == 0){
			deleteIt($row['id']);
		}else{
			if($row['revid'] != $webRay->parse->revid){
				logg("Version mismatch");
				//Version mismatch, need to update
				updateIt($row['id'],$webRay->parse->revid,$baseurl);
			}else{
				//Versions are the same
				markCheck($row['id']);
			}
		}
	}else{
		//Article isn't in DB
		populateIt($baseurl);
	}
} //End investigateIt

function updateOlder(){
	logg("updateOlder()");
	//Less than 24 hours old, check every 30  (Provided it hasn't already been touched by getLatesT()
	//24-48 hours old, check every 2 hours
	//48-72 hours, check every 4 hours
	//72-168 hours, check every 12 hours
	$sql = array();
	$sql[] = "SELECT * FROM `enwn_articles` WHERE `first_added` < now( ) AND `first_added` >= DATE_SUB( now( ) , INTERVAL 24 HOUR ) AND `last_check` <= DATE_SUB( now( ) , INTERVAL 30 MINUTE )";
	$sql[] = "SELECT * FROM `enwn_articles` WHERE `first_added` < DATE_SUB( now( ) , INTERVAL 24 HOUR ) AND `first_added` >= DATE_SUB( now( ) , INTERVAL 48 HOUR ) AND `last_check` <= DATE_SUB( now( ) , INTERVAL 2 HOUR )";
	$sql[] = "SELECT * FROM `enwn_articles` WHERE `first_added` < DATE_SUB( now( ) , INTERVAL 48 HOUR ) AND `first_added` >= DATE_SUB( now( ) , INTERVAL 72 HOUR ) AND `last_check` <= DATE_SUB( now( ) , INTERVAL 4 HOUR )";
	$sql[] = "SELECT * FROM `enwn_articles` WHERE `first_added` < DATE_SUB( now( ) , INTERVAL 72 HOUR ) AND `first_added` >= DATE_SUB( now( ) , INTERVAL 168 HOUR ) AND `last_check` <= DATE_SUB( now( ) , INTERVAL 12 HOUR )";

	for($a = 0;$a < count($sql); $a++){
		$fullsql = $sql[$a] . " AND `deleted`=FALSE AND `expired`=FALSE";
		logg($fullsql);
		$res = mysqli_query($GLOBALS['con'], $fullsql);
		logg(mysqli_error($GLOBALS['con']));
		while($row = mysqli_fetch_array($res)){
			logg($row['title']);
			investigateIt($row['url']);
		}
	}
}

function deleteIt($id) {
	logg("deleteIt($id)");
	$sql = "UPDATE `enwn_articles` SET `deleted`=TRUE WHERE `id`='$id'";
	logg($sql);
	$res = mysqli_query($GLOBALS['con'], $sql);
	logg(mysqli_error($GLOBALS['con']));
}

function cleanUpRedir() {
	logg("cleanUpRedir()");
	$sql = "SELECT `id` FROM `enwn_articles` WHERE `fulltext` like '#REDIRECT%' AND `deleted`=FALSE AND `last_check` >= DATE_SUB( now( ) , INTERVAL 2 HOUR )";
	logg($sql);
	$res = mysqli_query($GLOBALS['con'], $sql);
	logg(mysqli_error($GLOBALS['con']));	
	while($row = mysqli_fetch_array($res)){
		deleteIt($row['id']);
	}
}
